package cn.smartorigin.soas.frame.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterJob;
import java.sql.SQLException;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToolBar;
import javax.swing.table.DefaultTableModel;

import cn.smartorigin.soas.data.SqliteConnecter;
import cn.smartorigin.soas.data.TestData;
import cn.smartorigin.soas.frame.AddClient;
import cn.smartorigin.soas.frame.AddProduct;
import cn.smartorigin.soas.frame.ColorTableIF;
import cn.smartorigin.soas.frame.ImageButton;
import cn.smartorigin.soas.frame.JColorTable;
import cn.smartorigin.soas.frame.ModifyClientProduct;


/**
 * @author GMK
 *
 */
public class ProductListPage  extends Portlet implements ColorTableIF{
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int clientId;
	private ImageIcon icon_nomal1 = new ImageIcon("res/images/product_list_bg.png");
    private ImageIcon icon_selected1 = new ImageIcon("res/images/report_form_bg_selected.png");
    ImageIcon minimizeIcon = new ImageIcon("res/images/add_product.png");
    ImageIcon minimizeIcon_p = new ImageIcon("res/images/add_product_p.png");
    ImageIcon printIcon = new ImageIcon("res/images/print.png");
    ImageIcon printIcon_p = new ImageIcon("res/images/print_p.png");
    ImageButton btn1 = new ImageButton(icon_nomal1, icon_nomal1, icon_selected1);
	JColorTable   cTable;
	boolean mIsFullscreen;
	
    
    public ProductListPage(int id){

    	this.clientId = id;
    	this.setLayout(new BorderLayout());
    	this.add(btn1, BorderLayout.CENTER);
    	
    }
    
	@Override
	public ImageButton getImageButton() {
		return btn1;
	}
	
	/**
	 * 
	 */
	@Override
	public void showMode(boolean isFullscreen){
		if(isFullscreen){	
			DefaultTableModel model = new DefaultTableModel();
		    model.addColumn("序号");
		    model.addColumn("材料型号");
		    model.addColumn("表面处理方式");
		    model.addColumn("镀种");
		    model.addColumn("单价 / kg");
	    
		    try {
				TestData.queryProductList(new SqliteConnecter(), model, clientId);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}		    
		    cTable = new JColorTable(model);
		    JScrollPane mClistPanel= new JScrollPane(cTable);
		    
			this.removeAll();
	        JToolBar toolbar = super.getControlPanel();
			JLabel jl = new JLabel("产品列表");
			jl.setFont(new Font("宋体", Font.BOLD, 14));
	        toolbar.add(jl, BorderLayout.CENTER);
		    this.add(mClistPanel, BorderLayout.CENTER);
		    this.add(toolbar, BorderLayout.NORTH);
		    
			JPanel pl = new JPanel();
			//pl.setBackground(Color.white);
			pl.setLayout(new FlowLayout(FlowLayout.RIGHT, 5, 5));
	        ImageButton addClient = new ImageButton(minimizeIcon, minimizeIcon, minimizeIcon_p);
	        addClient.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					try {
						String clientName = TestData.getClientName(new SqliteConnecter(), clientId);
						new AddProduct(clientId, clientName , ProductListPage.this);
					} catch (ClassNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}

					
				}
	        	
	        });
			pl.add(addClient);
	        
	        ImageButton printBtn= new ImageButton(printIcon, printIcon, printIcon_p);
	        printBtn.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					//PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
					//for (int i = 0; i < services.length; i++)
					//{
					//	System.out.println(services[i].getName());
					//}
					
					PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
					PrintService svc = PrintServiceLookup.lookupDefaultPrintService();
					PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet();
					PrintService selection = ServiceUI.printDialog( null, 100, 100, services, svc, null, attrs);
				}
	        	
	        });
			pl.add(printBtn);
			this.add(pl,BorderLayout.SOUTH);
			
			
		}else{
			this.removeAll();
			this.add(btn1, BorderLayout.CENTER);
		}
	}
	

	@Override
	public void TableModify(int rowId) {
		// TODO Auto-generated method stub
		//System.out.println("Modify" + rowId);
		String productName =  (String)cTable.getValueAt(rowId, 1);
		String productprice = (String)cTable.getValueAt(rowId, 4);
		try {
			String clientName = TestData.getClientName(new SqliteConnecter(), clientId);
			new  ModifyClientProduct(clientId, clientName, productName, productprice, ProductListPage.this);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}



	}

	@Override
	public void TableDelete(int rowId) {
		// TODO Auto-generated method stub
		//System.out.println("Delete" + rowId);	
		String productName =  (String)cTable.getValueAt(rowId, 1);
		try {
			TestData.deleteClientProduct(new SqliteConnecter(), clientId, productName);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		showMode(true);
		this.revalidate();
		this.repaint();
		
	}
	
	public void flashData(){
		showMode(true);
	}

	
}
